home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 16 / AMIGAplus Sonderheft 16 (1998)(ICP)(DE)[!].iso / pd / anwendungen / ispell-3.1.18bin / interfaces / guispell-1.1 / minrexx.c < prev    next >
C/C++ Source or Header  |  1995-09-21  |  17KB  |  501 lines

  1. /*
  2.  *   This is an example of how REXX messages might be handled.  This is
  3.  *   a `minimum' example that both accepts asynchronous REXX messages and
  4.  *   can request REXX service [and can issue simple REXX commands to other
  5.  *   rexx servers -ljr].
  6.  *
  7.  *   Read this entire file!  It's short enough.
  8.  *
  9.  *   It is written in such a fashion that it can be attached to a program
  10.  *   with a minimum of fuss.  The only external symbols it makes available
  11.  *   are the [eight] functions.
  12.  *   [Note that the application must now declare, open, and close
  13.  *    RexxSysBase. -ljr]
  14.  *
  15.  *   This code is by Radical Eye Software, but it is put in the public
  16.  *   domain.  I would appreciate it if the following string was left in
  17.  *   both as a version check and as thanks from you for the use of this
  18.  *   code.
  19.  *
  20.  *   If you modify this file for your own use, don't bump the version
  21.  *   number; add a suffix, such as 1.0a or 1.0.3 or something, so we
  22.  *   don't have fake `versions' floating around.
  23.  *
  24.  *   Simple command issuing extensions by
  25.  *   Loren J. Rittle  Thu Dec 05 01:33:46 1991
  26.  */
  27. static char *blurb =
  28. "Radical Eye MinRexx 0.4ljr (with simple command issuing extensions)";
  29.  
  30. /*
  31.  *  The application *must* declare, open (before calling any minrexx code),
  32.  *  and close (after calling any/all minrexx code) RexxSysBase. -ljr
  33.  */
  34.  
  35. /*
  36.  *   We read in our own personal little include.
  37.  */
  38. #include <string.h>
  39. #pragma msg 148 ignore push
  40. #pragma msg 149 ignore push
  41. #pragma msg 61 ignore push
  42. #include <proto/exec.h>
  43. #pragma msg 149 pop
  44. #pragma msg 61 pop
  45. #include "libraries.h"
  46. #include "minrexx.h"
  47.  
  48. static int cmdcmp (char *c, char *m);
  49. static void replytoit (struct RexxMsg *msg);
  50.  
  51. /*
  52.  *   All of our local globals, hidden from sight.
  53.  */
  54. static struct MsgPort *rexxPort;/* this is *our* rexx port */
  55. static int bringerdown;        /* are we trying to shut down? */
  56. static struct rexxCommandList *globalrcl;    /* our command association list */
  57. static long stillNeedReplies;    /* how many replies are pending? */
  58. static long rexxPortBit;    /* what bit to wait on for Rexx? */
  59. static char *extension;        /* the extension for macros */
  60. static void (*userdisp) (struct RexxMsg *, struct rexxCommandList *, char *);    /* the user's dispatch function */
  61. static struct RexxMsg *oRexxMsg;/* the outstanding Rexx message */
  62.  
  63. /*
  64.  *   This is the main entry point into this code.
  65.  */
  66. long upRexxPort (char *s, struct rexxCommandList *rcl, char *exten, void(*uf)(struct RexxMsg *, struct rexxCommandList *, char *))
  67. /*
  68.  *   The first argument is the name of your port to be registered;
  69.  *   this will be used, for instance, with the `address' command of ARexx.
  70.  *   The second argument is an association list of command-name/user-data
  71.  *   pairs.  It's an array of struct rexxCommandList, terminated by a
  72.  *   structure with a NULL in the name field. The commands are case
  73.  *   sensitive.  The user-data field can contain anything appropriate,
  74.  *   perhaps a function to call or some other data.
  75.  *   The third argument is the file extension for ARexx macros invoked
  76.  *   by this program.  If you supply this argument, any `primitive' not
  77.  *   in the association list rcl will be sent out to ARexx for
  78.  *   interpretation, thus allowing macro programs to work just like
  79.  *   primitives.  If you do not want this behavior, supply a `NULL'
  80.  *   here, and those commands not understood will be replied with an
  81.  *   error value of RXERRORNOCMD.
  82.  *   The fourth argument is the user dispatch function.  This function
  83.  *   will *only* be called from rexxDisp(), either from the user calling
  84.  *   this function directly, or from dnRexxPort().  Anytime a command
  85.  *   match is found in the association list, this user-supplied function
  86.  *   will be called with two arguments---the Rexx message that was
  87.  *   received, and a pointer to the association pair.  This function
  88.  *   should return a `1' if the message was replied to by the function
  89.  *   and a `0' if the default success code of (0, 0) should be returned.
  90.  *   Note that the user function should never ReplyMsg() the message;
  91.  *   instead he should indicate the return values with replyRexxCmd();
  92.  *   otherwise we lose track of the messages that still lack replies.
  93.  *   upRexxPort() returns the signal bit to wait on for Rexx messages.
  94.  *   If something goes wrong, it simply returns a `0'.  Note that this
  95.  *   function is safe to call multiple times because we check to make
  96.  *   sure we haven't opened already.  It's also a quick way to change
  97.  *   the association list or dispatch function.
  98.  */
  99. {
  100.  
  101. /*
  102.  *   Some basic error checking.
  103.  */
  104.   if (rcl == NULL || uf == NULL)
  105.     return (0L);
  106. /*
  107.  *   If we aren't open, we make sure no one else has opened a port with
  108.  *   this name already.  If that works, and the createport succeeds, we
  109.  *   fill rexxPortBit with the value to return.
  110.  *
  111.  *   Note that rexxPortBit will be 0 iff rexxPort is NULL, so the check
  112.  *   for rexxPort == NULL also insures that our rexxPortBit is 0.
  113.  */
  114.   if (rexxPort == NULL)
  115.     {
  116.       Forbid ();
  117.       if (FindPort (s) == NULL)
  118.     rexxPort = CreatePort (s, 0L);
  119.       Permit ();
  120.       if (rexxPort != NULL)
  121.     rexxPortBit = 1L << rexxPort->mp_SigBit;
  122.     }
  123. /*
  124.  *   Squirrel away these values for our own internal access, and return
  125.  *   the wait bit.
  126.  */
  127.   globalrcl = rcl;
  128.   extension = exten;
  129.   userdisp = uf;
  130.   return (rexxPortBit);
  131. }
  132.  
  133. /*
  134.  *   This function closes down the Rexx port.  It is always safe to
  135.  *   call, and should *definitely* be made a part of your cleanup
  136.  *   routine.  No arguments and no return.  It removes the Rexx port,
  137.  *   replies to all of the messages and insures that we get replies
  138.  *   to all the ones we sent out, closes the Rexx library, deletes the
  139.  *   port, clears a few flags, and leaves.
  140.  */
  141. void dnRexxPort (void)
  142. {
  143.   if (rexxPort)
  144.     {
  145.       RemPort (rexxPort);
  146.       bringerdown = 1;
  147. /*
  148.  *   A message still hanging around?  We kill it off.
  149.  */
  150.       if (oRexxMsg)
  151.     {
  152.       oRexxMsg->rm_Result1 = RXERRORIMGONE;
  153.       ReplyMsg ((struct Message *)oRexxMsg);
  154.       oRexxMsg = NULL;
  155.     }
  156.       while (stillNeedReplies)
  157.     {
  158.       WaitPort (rexxPort);
  159.       dispRexxPort ();
  160.     }
  161.       DeletePort (rexxPort);
  162.       rexxPort = NULL;
  163.     }
  164.   rexxPortBit = 0;
  165. }
  166.  
  167. /*
  168.  *   Here we dispatch any REXX messages that might be outstanding.
  169.  *   This is the main routine for handling Rexx messages.
  170.  *   This function is fast if no messages are outstanding, so it's
  171.  *   pretty safe to call fairly often.
  172.  *
  173.  *   If we are bring the system down and flushing messages, we reply
  174.  *   with a pretty serious return code RXERRORIMGONE.
  175.  *
  176.  *   No arguments, no returns.
  177.  */
  178. void dispRexxPort (void)
  179. {
  180.   register struct RexxMsg *RexxMsg;
  181.   register struct rexxCommandList *rcl;
  182.   register char *p;
  183.   register int dontreply;
  184.  
  185. /*
  186.  *   If there's no rexx port, we're out of here.
  187.  */
  188.   if (rexxPort == NULL)
  189.     return;
  190. /*
  191.  *   Otherwise we have our normal loop on messages.
  192.  */
  193.   while (RexxMsg = (struct RexxMsg *) GetMsg (rexxPort))
  194.     {
  195. /*
  196.  *   If we have a reply to a message we sent, we look at the second
  197.  *   argument.  If it's set, it's a function we are supposed to call
  198.  *   so we call it.  Then, we kill the argstring and the message
  199.  *   itself, decrement the outstanding count, and attempt to close
  200.  *   down the Rexx library.  Note that this call only succeeds if
  201.  *   there are no outstanding messages.  Also, it's pretty quick, so
  202.  *   don't talk to me about efficiency.
  203.  */
  204.       if (RexxMsg->rm_Node.mn_Node.ln_Type == NT_REPLYMSG)
  205.     {
  206.       if (RexxMsg->rm_Args[1])
  207.         {
  208.           (*((int (*) (struct RexxMsg *)) (RexxMsg->rm_Args[1]))) (RexxMsg);
  209.         }
  210.       DeleteArgstring (RexxMsg->rm_Args[0]);
  211.       DeleteRexxMsg (RexxMsg);
  212.       stillNeedReplies--;
  213. /*
  214.  *   The default case is we got a message and we need to check it for
  215.  *   primitives.  We skip past any initial tabs or spaces and initialize
  216.  *   the return code fields.
  217.  */
  218.     }
  219.       else
  220.     {
  221.       p = (char *) RexxMsg->rm_Args[0];
  222.       while (*p > 0 && *p <= ' ')
  223.         p++;
  224.       RexxMsg->rm_Result1 = 0;
  225.       RexxMsg->rm_Result2 = 0;
  226. /*
  227.  *   If somehow the reply is already done or postponed, `dontreply' is
  228.  *   set.
  229.  */
  230.       dontreply = 0;
  231. /*
  232.  *   If the sky is falling, we just blow up and replymsg.
  233.  */
  234.       if (bringerdown)
  235.         {
  236.           RexxMsg->rm_Result1 = RXERRORIMGONE;
  237. /*
  238.  *   Otherwise we cdr down our association list, comparing commands,
  239.  *   until we get a match.  If we get a match, we call the dispatch
  240.  *   function with the appropriate arguments, and break out.
  241.  */
  242.         }
  243.       else
  244.         {
  245.           oRexxMsg = RexxMsg;
  246.           for (rcl = globalrcl; rcl->name; rcl++)
  247.         {
  248.           if (cmdcmp (rcl->name, p) == 0)
  249.             {
  250.               if (p[strlen (rcl->name)])
  251.             (*userdisp) (RexxMsg, rcl, p + strlen (rcl->name) + 1);
  252.               else
  253.             (*userdisp) (RexxMsg, rcl, p + strlen (rcl->name));
  254.               break;
  255.             }
  256.         }
  257. /*
  258.  *   If we broke out, rcl will point to the command we executed; if we
  259.  *   are at the end of the list, we didn't understand the command.  In
  260.  *   this case, if we were supplied an extension in upRexxPort, we know
  261.  *   that we should send the command out, so we do so, synchronously.
  262.  *   The synchronous send takes care of our reply.  If we were given a
  263.  *   NULL extension, we bitch that the command didn't make sense to us.
  264.  */
  265.           if (rcl->name == NULL)
  266.         {
  267.           if (extension)
  268.             {
  269.               syncRexxCmd (RexxMsg->rm_Args[0], RexxMsg);
  270.               dontreply = 1;
  271.             }
  272.           else
  273.             {
  274.               RexxMsg->rm_Result1 = RXERRORNOCMD;
  275.             }
  276.         }
  277.         }
  278. /*
  279.  *   Finally, reply if appropriate.
  280.  */
  281.       oRexxMsg = NULL;
  282.       if (!dontreply)
  283.         ReplyMsg ((struct Message *)RexxMsg);
  284.     }
  285.     }
  286. }
  287.  
  288. /*
  289.  *   This is the function we use to see if the command matches
  290.  *   the command string.  Not case sensitive.  Make sure all commands
  291.  *   are given in lower case!
  292.  */
  293. static int cmdcmp (char *c, char *m)
  294. {
  295.   while (*c && ((*c == *m) || (*c == *m + 32 && ('a' <= *c && *c <= 'z'))))
  296.     {
  297.       c++;
  298.       m++;
  299.     }
  300.   if (!(*c) && *m)
  301.     return ((int) *m != ' ');
  302.   return ((int) *c);
  303. }
  304.  
  305. /*
  306.  *   This is the general ARexx command interface, but is not the one
  307.  *   you will use most of the time; ones defined later are easier to
  308.  *   understand and use.  But they all go through here.
  309.  */
  310. struct RexxMsg *sendRexxCmd (char *s, void (*f)(struct RexxMsg *), STRPTR p1, STRPTR p2, STRPTR p3)
  311. /*
  312.  *   The first parameter is the command to send to Rexx.
  313.  *   The second parameter is either NULL, indicating that the command
  314.  *   should execute asynchronously, or a function to be called when the
  315.  *   message we build up and send out here finally returns.  Please note
  316.  *   that the function supplied here could be called during cleanup after
  317.  *   a fatal error, so make sure it is `safe'.  This function always is
  318.  *   passed one argument, the RexxMsg that is being replied.
  319.  *   These are up to three arguments to be stuffed into the RexxMsg we
  320.  *   are building up, making the values available when the message is
  321.  *   finally replied to.  The values are stuffed into Args[2]..Args[4].
  322.  */
  323. {
  324.   register struct MsgPort *rexxport;
  325.   register struct RexxMsg *RexxMsg;
  326.  
  327. /*
  328.  *   If we have too many replies out there, we just return failure.
  329.  *   Note that you should check the return code to make sure your
  330.  *   message got out!  Then, we forbid, and make sure that:
  331.  *      - we have a rexx port open
  332.  *      - Rexx is out there
  333.  *      - the library is open
  334.  *      - we can create a message
  335.  *      - we can create an argstring
  336.  *
  337.  *   If all of these succeed, we stuff a few values and send the
  338.  *   message, permit, and return.
  339.  */
  340.   if (rexxPort == NULL || stillNeedReplies > MAXRXOUTSTANDING - 1)
  341.     return (NULL);
  342.   if ((RexxMsg = CreateRexxMsg (rexxPort, extension, rexxPort->mp_Node.ln_Name)) &&
  343.       (RexxMsg->rm_Args[0] = CreateArgstring (s, strlen (s))))
  344.     {
  345.       RexxMsg->rm_Action = RXCOMM;
  346.       RexxMsg->rm_Args[1] = (STRPTR) f;
  347.       RexxMsg->rm_Args[2] = p1;
  348.       RexxMsg->rm_Args[3] = p2;
  349.       RexxMsg->rm_Args[4] = p3;
  350.       RexxMsg->rm_Node.mn_Node.ln_Name = RXSDIR;
  351.       Forbid ();
  352.       if (rexxport = FindPort (RXSDIR))
  353.     PutMsg (rexxport, (struct Message *)RexxMsg);
  354.       Permit ();
  355.       if (rexxport)
  356.     {
  357.       stillNeedReplies++;
  358.       return (RexxMsg);
  359.     }
  360.       else
  361.     DeleteArgstring (RexxMsg->rm_Args[0]);
  362.     }
  363.   if (RexxMsg)
  364.     DeleteRexxMsg (RexxMsg);
  365.   return (NULL);
  366. }
  367.  
  368. /*
  369.  *   This function is used to send out an ARexx message and return
  370.  *   immediately.  Its single parameter is the command to send.
  371.  */
  372. struct RexxMsg *asyncRexxCmd (char *s)
  373. {
  374.   return (sendRexxCmd (s, NULL, NULL, NULL, NULL));
  375. }
  376.  
  377. /*
  378.  *   This function sets things up to reply to the message that caused
  379.  *   it when we get a reply to the message we are sending out here.
  380.  *   But first the function we pass in, which actually handles the reply.
  381.  *   Note how we get the message from the Args[2]; Args[0] is the command,
  382.  *   Args[1] is this function, and Args[2]..Args[4] are any parameters
  383.  *   passed to sendRexxCmd() as p1..p3.  We pass the result codes right
  384.  *   along.
  385.  */
  386. static void replytoit (struct RexxMsg *msg)
  387. {
  388.   register struct RexxMsg *omsg;
  389.  
  390.   omsg = (struct RexxMsg *) (msg->rm_Args[2]);
  391.   replyRexxCmd (omsg, msg->rm_Result1, msg->rm_Result2, NULL);
  392.   ReplyMsg ((struct Message *)omsg);
  393. }
  394.  
  395. /*
  396.  *   This function makes use of everything we've put together so far,
  397.  *   and functions as a synchronous Rexx call; as soon as the macro
  398.  *   invoked here returns, we reply to `msg', passing the return codes
  399.  *   back.
  400.  */
  401. struct RexxMsg *syncRexxCmd (char *s, struct RexxMsg *msg)
  402. {
  403.   return (sendRexxCmd (s, &replytoit, (STRPTR)msg, NULL, NULL));
  404. }
  405.  
  406. /*
  407.  *   There are times when you want to pass back return codes or a
  408.  *   return string; call this function when you want to do that,
  409.  *   and return `1' from the user dispatch function so the main
  410.  *   event loop doesn't reply (because we reply here.)  This function
  411.  *   always returns 1.
  412.  */
  413. void replyRexxCmd (struct RexxMsg *msg, long primary, long secondary, char *string)
  414. /*
  415.  *   The first parameter is the message we are replying to.
  416.  *   The next two parameters are the primary and secondary return
  417.  *   codes.
  418.  *   The final parameter is a return string.  This string is only
  419.  *   returned if the primary return code is 0, and a string was
  420.  *   requested.
  421.  *
  422.  *   We also note that we have replied to the message that came in.
  423.  */
  424. {
  425. /*
  426.  *   Note how we make sure the Rexx Library is open before calling
  427.  *   CreateArgstring . . . and we close it down at the end, if possible.
  428.  */
  429.   if (primary == 0 && (msg->rm_Action & (1L << RXFB_RESULT)))
  430.     {
  431.       if (string)
  432.     secondary = (long) CreateArgstring (string, strlen (string));
  433.       else
  434.     secondary = 0L;
  435.     }
  436.   msg->rm_Result1 = primary;
  437.   msg->rm_Result2 = secondary;
  438. }
  439.  
  440. /*
  441.  *   This is the general simple command interface. Added by Loren J. Rittle
  442.  */
  443. struct RexxMsg *sendSimpleCmd (char *s, char *host, void (*f)(struct RexxMsg *), STRPTR p1, STRPTR p2, STRPTR p3)
  444. /*
  445.  *   The first parameter is the command to send to the host.
  446.  *   The second parameter is the name of the host to send to.
  447.  *   The thrid parameter is either NULL, indicating that the command
  448.  *   should execute asynchronously, or a function to be called when the
  449.  *   message we build up and send out here finally returns.  Please note
  450.  *   that the function supplied here could be called during cleanup after
  451.  *   a fatal error, so make sure it is `safe'.  This function always is
  452.  *   passed one argument, the RexxMsg that is being replied.
  453.  *   These are up to three arguments to be stuffed into the RexxMsg we
  454.  *   are building up, making the values available when the message is
  455.  *   finally replied to.  The values are stuffed into Args[2]..Args[4].
  456.  */
  457. {
  458.   register struct MsgPort *rexxport;
  459.   register struct RexxMsg *RexxMsg;
  460.  
  461. /*
  462.  *   If we have too many replies out there, we just return failure.
  463.  *   Note that you should check the return code to make sure your
  464.  *   message got out!  Then, we forbid, and make sure that:
  465.  *      - we have a rexx port open
  466.  *      - Rexx is out there
  467.  *      - the library is open
  468.  *      - we can create a message
  469.  *      - we can create an argstring
  470.  *
  471.  *   If all of these succeed, we stuff a few values and send the
  472.  *   message, permit, and return.
  473.  */
  474.   if (rexxPort == NULL || stillNeedReplies > MAXRXOUTSTANDING - 1)
  475.     return (NULL);
  476.   if ((RexxMsg = CreateRexxMsg (rexxPort, extension, rexxPort->mp_Node.ln_Name)) &&
  477.       (RexxMsg->rm_Args[0] = CreateArgstring (s, strlen (s))))
  478.     {
  479.       RexxMsg->rm_Action = RXCOMM | RXFF_NOIO | RXFF_RESULT | RXFF_STRING;
  480.       RexxMsg->rm_Args[1] = (STRPTR) f;
  481.       RexxMsg->rm_Args[2] = p1;
  482.       RexxMsg->rm_Args[3] = p2;
  483.       RexxMsg->rm_Args[4] = p3;
  484.       RexxMsg->rm_Node.mn_Node.ln_Name = host;
  485.       Forbid ();
  486.       if (rexxport = FindPort (host))
  487.     PutMsg (rexxport, (struct Message *)RexxMsg);
  488.       Permit ();
  489.       if (rexxport)
  490.     {
  491.       stillNeedReplies++;
  492.       return (RexxMsg);
  493.     }
  494.       else
  495.     DeleteArgstring (RexxMsg->rm_Args[0]);
  496.     }
  497.   if (RexxMsg)
  498.     DeleteRexxMsg (RexxMsg);
  499.   return (NULL);
  500. }
  501.